const MYSQL = require("../../sql/mysql");
const Redis = require("../../sql/redis");
/**
 * 获取设备列表
 * @return {promise<{code:string,msg:string}>}
 */
async function GetDeviceList(Request) {
    try {
        let user = Request.$user;
        let query = Request.query;
        let { page = 1 } = query;
        let limit = 10;
        const conn = await MYSQL.promise().getConnection();
        // 获取列表数据
        let mysqlResult = await conn.query(`
          SELECT * FROM ${deviceSqlName} WHERE user = '${user}'
          LIMIT ${(page - 1) * limit},${limit}
          `);
        return {
            code: 0,
            msg: "获取成功",
            data: {
                page: page,
                limit,
                list: mysqlResult[0],
                total: mysqlResult[1].length,
            },
        };
    } catch (error) {
        throw { code: 500, msg: "获取失败" };
    }
}
/**
 * 删除设备
 * @return {promise<{code:string,msg:string}>}
 */
async function DeleteDevice(Request) {
    try {
        let user = Request.$user;
        let body = Request.body;
        let { id } = body;
        if (!id) {
            throw { code: 500, msg: "id不能为空" };
        }
        const conn = await MYSQL.promise().getConnection();
        await conn.query(
            `DELETE FROM ${deviceSqlName} WHERE id = '${id}' and user = '${user}'`
        );
        return { code: 0, msg: "删除成功" };
    } catch (error) {
        throw { code: 500, msg: "删除失败" };
    }
}
module.exports = {
    GetDeviceList,
    DeleteDevice
}